<?php

/**
 * This is the model class for table "{{slider}}".
 *
 * The followings are the available columns in table '{{slider}}':
 * @property integer $id
 * @property integer $service_id
 * @property string $images
 * @property integer $rank
 * @property float $price_from
 * @property integer $status
 */
class Slider extends CActiveRecord
{

    const STATUS_ACTIVE = 1;
    const STATUS_BLOCK  = 0;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Slider the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{slider}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('service_id, status, images', 'required'),
            array('service_id, status, rank', 'numerical', 'integerOnly' => true),
            array('price_from', 'numerical'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, service_id, price_from, status,images, rank', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'service' => array(self::BELONGS_TO, 'Service', 'service_id')
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id'         => 'ID',
            'service_id' => Yii::t('main', 'Service'),
            'images'     => Yii::t('main', 'Images'),
            'rank'       => Yii::t('main', 'Rank'),
            'price_from' => Yii::t('main', 'Price From'),
            'status'     => Yii::t('main', 'Status'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('service_id', $this->service_id);
        $criteria->compare('images', $this->images);
        $criteria->compare('rank', $this->rank);
        $criteria->compare('price_from', $this->price_from);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function getArrayStatus()
    {
        return array(
            self::STATUS_ACTIVE => Yii::t('main', 'Active'),
            self::STATUS_BLOCK  => Yii::t('main', 'Block'),
        );
    }

    public function getStatusLabel()
    {
        if (!$this->hasAttribute("status"))
            return '';

        $labelClass = 'label ';
        switch ($this->status)
        {
            case self::STATUS_ACTIVE:
                $labelClass .= 'label-success';
                break;

            case self::STATUS_BLOCK:
                $labelClass .= 'label-important';
                break;

            default:
                break;
        }
        $labels = self::getArrayStatus();
        $label  = isset($labels[$this->status]) ? $labels[$this->status] : $this->status;
        return "<span class='$labelClass'>$label</span>";
    }

    public function getPublicImages()
    {
        if ($this->images)
            return Yii::app()->baseUrl . "/" . $this->images;
        else
            return Yii::app()->controller->assetDir . '/img/no-image.png';
    }

    public function getImageBySize($width, $height)
    {
        if (!$this->images OR $this->images == '')
        {
            return Yii::app()->controller->assetDir . '/img/no-image.png';
        }

        $file = Yii::getPathOfAlias('webroot') . '/' . $this->images;
        $path = Yii::getPathOfAlias('webroot') . '/files/service/';

        if (!file_exists($file) and ! is_file($file))
        {
            return Yii::app()->controller->assetDir . '/img/no-image.png';
        }

        $path_parts = pathinfo($file);
        $extesion   = $path_parts['extension'];
        $file_name  = $path_parts['filename'] . "_w" . $width . 'h' . $height . '.' . $extesion;

        if (!file_exists($path . $file_name) AND ! is_file($path . $file_name))
        {
            $thumb = Yii::app()->phpThumb->create($file);
            $thumb->adaptiveResize($width, $height);
            $thumb->save($path . $file_name);
        }

        return Yii::app()->baseUrl . '/files/service' . '/' . $file_name;
    }

}
